package com.fingard.dsp.bank.directbank.citic02;

import com.fingard.dsp.bank.directbank.bankInterface.IPrivateSaleMsg;

import com.fingard.dsp.bank.reqrespobj.privatesale.ReqPrivateSaleMsgQry;
import com.fingard.dsp.bank.reqrespobj.privatesale.RetAtsPrivateSaleDetail;
import com.fingard.dsp.bank.reqrespobj.privatesale.RetAtsPrivateSaleMsg;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by aaron on 2020/5/11.
 */
public class CITIC02PrivateSaleMsg extends CITIC02Base implements IPrivateSaleMsg {
    @Override
    public RetAtsPrivateSaleMsg getPrivateSaleMsg(ReqPrivateSaleMsgQry qry) throws Exception {
        RetAtsPrivateSaleMsg retAts = new RetAtsPrivateSaleMsg(qry.reqHeader);
        retAts.writeStart();
        String tmpRespCode = "";
        String tmpRespInfo = "";
        try {
            int page =1;
            int hasReadNum =0;
            int totalNum =0;
            do {
                CITIC02Header header = new CITIC02Header();
                header.stdprocode = "RLTMSG";
                header.msgsendertrc = reqHeader.transSeqID;
                header.trdcode = "CPCCSDPQ";
                //            封装body内容
                XmlTextWriter xmlWriterBody = new XmlTextWriter();
                xmlWriterBody.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
                xmlWriterBody.writeStartElement("stream");
//            LONCPID	企业端经销商客户代码	VARCHAR	20	Y
                xmlWriterBody.writeElementString("LONCPID", qry.DealerCode);
//            BKCUSTCD	银行端借款企业ID	VARCHAR	20	N
                xmlWriterBody.writeElementString("BKCUSTCD", "");
//            LONCPNM	借款企业名称	VARCHAR	100	Y
                xmlWriterBody.writeElementString("LONCPNM", qry.DealerName);
//            OPBRHCD	经办分行编号	VARCHAR	10	N
                xmlWriterBody.writeElementString("OPBRHCD", "");
//            OPBRHNM	经办分行名称	VARCHAR	100	N
                xmlWriterBody.writeElementString("OPBRHNM", "");
//            OUTDT	出库日期/反馈日期	VARCHAR	30	N	"“YYYYMMDD”查出库信息时出库日期必填； 查异常车辆信息时反馈日期必填，t-1日日期。"
                xmlWriterBody.writeElementString("OUTDT", "");
//            QUERYTP	查询类型	VARCHAR	1	Y	1-出库 2-在库，3-异常车辆状态
                xmlWriterBody.writeElementString("QUERYTP", "3");
//                PAGESIZE	每页记录数	VARCHAR	10	Y
                xmlWriterBody.writeElementString("PAGESIZE", "200");
//                PAGENO	当前页码	VARCHAR	10	Y
                xmlWriterBody.writeElementString("PAGENO", (page++)+"");
//            FIELD1	属性1	VARCHAR	30	N
                xmlWriterBody.writeElementString("FIELD1", "");
//            FIELD2	属性2	VARCHAR	60	N
                xmlWriterBody.writeElementString("FIELD2", "");
//            FIELD3	属性3	VARCHAR	100	N
                xmlWriterBody.writeElementString("FIELD3", "");

                xmlWriterBody.writeEndDocument();
                String[] tmpStrRet = sendToBank2(xmlWriterBody.toXmlString(), header);
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    //解析银行返回
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String tmpRetCode = tmpBkReader.getFirstText("/stream/RETCODE");
                        if ("AAAAAAA".equals(tmpRetCode)) {
                            if(totalNum==0){
                                tmpRespCode = "0";
                                tmpRespInfo = "获取成功";
                                totalNum =Integer.valueOf(tmpBkReader.getFirstText("/stream/TTLNUM"));
                            }
                            while (tmpBkReader.read("/stream/list/row")) {
                                hasReadNum++;
//                                SUPST	监管状态	VARCHAR	2	Y	1-私移，2-私售;查异常车辆信息时必填
                                String status =  tmpBkReader.readedXElement.getSingleText("SUPST");
                                if(!"2".equalsIgnoreCase(status)){
                                    continue;
                                }

                                RetAtsPrivateSaleDetail detail = new RetAtsPrivateSaleDetail();
                                detail.DEALER_CODE = qry.CHANNEL_CODE;
                                detail.DEALER_NAME = qry.DealerName;
//                                CHASNO	车架号	VARCHAR	30	Y	查在库车辆信息\出库车辆\异常车辆信息时必填
                                detail.VIN = tmpBkReader.readedXElement.getSingleText("CHASNO");
//                                CARSTAT	汽车状态	VARCHAR	20	Y	"1-在库；2-已出库； 查在库车辆信息和出库车辆信息时必填"
//                                CPWHCD	企业端仓库代码	VARCHAR	30	N
//                                BKWHCD	银行端仓库代码	VARCHAR	30	Y	查在库车辆信息和异常车辆信息时必填
//                                SPVWHNAM	监管仓库名称	VARCHAR	200	Y	查在库车辆信息和异常车辆信息时必填
//                                WHLEV	仓库级别	VARCHAR	20	Y	1：一级仓库，2：二级仓库；查在库车辆信息和异常车辆信息时必填
//                                SPVWHADD	监管仓库地址	VARCHAR	200	N
                                detail.warehouseName = tmpBkReader.readedXElement.getSingleText("SPVWHADD");
//                                SPVWHCON	监管仓库联系人	VARCHAR	100	N
//                                SPVWHTEL	监管仓库联系电话	VARCHAR	20	N
//                                PLDNAME	押品名称	VARCHAR	80	N	车型
//                                PLDPRICE	押品单价	DECIMAL	19,2	Y	"车价；
                                detail.CAR_PRICE = tmpBkReader.readedXElement.getSingleText("PLDPRICE");
//                                查在库车辆信息和出库车辆信息时必填"
//                                PLDUNIT	押品单位	VARCHAR	2	Y	"1-份，2-克，3-公斤，4-吨，5-辆，默认为5-辆；
//                                查在库车辆信息和出库车辆信息时必填"
//                                HGZNO	合格证号	VARCHAR	60	N
                                detail.kchgzh = tmpBkReader.readedXElement.getSingleText("HGZNO");
//                                INDT	入库日期	VARCHAR	30	Y	查在库车辆信息和出库车辆信息时必填
//                                ENGINENO	发动机号	VARCHAR	60	N
                                detail.kcfdjh= tmpBkReader.readedXElement.getSingleText("ENGINENO");
                                detail.moniStatus= "私售";
//                                SUPRMK	监管方备注	VARCHAR	30	N
//                                FINCNO	融资编号	VARCHAR	60	N	查在库车辆信息时提供
//                                FINCEDDT	融资到期日	VARCHAR	20	N	“YYYYMMDD”查在库车辆信息时提供
//                                FIELD4	属性4	VARCHAR	30	N
//                                FIELD5	属性5	VARCHAR	50	N
//                                FIELD6	属性6	VARCHAR	100	N
                                retAts.writeRetDetail(detail);
                            }
                        } else {
                            tmpRespCode = "-1";
                            tmpRespInfo = tmpRetCode;
                        }
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = tmpSbMsg.toString();
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpStrRet[1];
                }
            }while (hasReadNum<totalNum);
        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        retAts.writeRespCode(tmpRespCode);
        retAts.writeRespInfo(tmpRespInfo);
        retAts.writeEnd();
        return retAts;
    }
}
